const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const { CleanWebpackPlugin  } = require('clean-webpack-plugin')
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin')
module.exports = {
  mode: 'none',
  devtool: 'source-map',
  entry: {
    main: path.resolve(__dirname, 'src/js/main.js')
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.js',
    // 打包前清空目录  webpack5.0
    // clean: true
  },
  // 插件
  plugins: [
    // 打包模板文件的
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, 'src/html/index.html')
    }),
    // 打包独立css文件
    new MiniCssExtractPlugin(),
    // 打包插件
    new CleanWebpackPlugin()
  ],
  // 模块
  module: {
    // 规则
    rules: [
      // loader  处理scss || css
      {
        test: /\.s?css$/i,
        use: [
          MiniCssExtractPlugin.loader  /*抽离独立的css文件*/
          // style-loader  创建style 动态插入css
        , 'css-loader', 'sass-loader']
      },
      // 处理 图片
      {
        test: /\.(jpg|png|gif)$/i,
        // webpack 5.0之前的写法  
        // use: ['file-loader']
        // webpack 5.0
        type: 'asset/resource'
      },
      // 处理图片
      {
        test: /\.html$/,
        loader: 'html-loader'
      },
      // 处理 js 
      {
        test: /\.js$/,
        exclude: /node_module/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }

      }
    ]
  },
  // 解析
  resolve: {
    // 配置别名
    alias: {
      // @ => 绝对路径 src
      "@": path.resolve(__dirname, 'src')
    },
    // 自动补全扩展名
    extensions: ['.js', '.scss', '.css', '.jpg']
  },
  optimization: {
    minimize: true,
    minimizer: [
      new CssMinimizerWebpackPlugin()
    ]
  }
}